"""
Copyright (c) 2023 - present SKbioinfo
"""
from xml.dom.minidom import Identified
from flask_restful import Resource
from flask import request,jsonify
from flask_login import (
    current_user,
    login_user,
    logout_user
)
from flask_jwt_extended import create_access_token,create_refresh_token
from apps.API.utils.log_helper import lg
from apps.API.connector.db_common import *
from apps.API.common.common_time import *
from apps.API.common.db_table_config import *

from apps.API.auth.models import Role,Users
from apps.API.auth.util import verify_pass

class interfaceLogin(Resource):
    def get(self):
        try:
            Role.init_role()
            Users.init_user()
            return 202
        except Exception as e:
            lg.error(e)
            return 404
    
    def post(self):
        username=request.json["username"]
        password=request.json["password"]
        user = Users.query.filter_by(username=username).first()
        if user and verify_pass(password, user.password):
            access_token = create_access_token(identity=username)
            refresh_token = create_refresh_token(identity=username)
            ### SQL: query user info ###
            query_user_sql = '''
                SELECT 
                    tb1.username,
                    tb1.email,
                    tb1.role_id,
                    tb2.group,
                    tb2.detail,
                    tb2.permissions
                FROM {table_one} as tb1
                LEFT JOIN {table_two} as tb2
                ON tb1.role_id=tb2.name
                WHERE tb1.username = \"{username}\"
            '''.format(
                table_one=common_tables.users_db["name"],
                table_two=common_tables.role_db["name"],
                username=username
            )
            user_info=dic_query_info(query_user_sql)[0]
            login_user(user)
            return {
                "access_token": access_token,
                "refresh_token":refresh_token,
                "username": user_info["username"],
                "group": user_info["group"],
                "role_id": user_info["role_id"],
                "detail": user_info["detail"],
                "permissions": user_info["permissions"]
            },202
        if not current_user.is_authenticated:
            return 400
        return 202
        
def logout():
    logout_user()
    return 202

class interfaceResigster(Resource):
    def post(self):
        pass
        # if 'register' in request.form:
        #     username = request.form['username']
        # email = request.form['email']
        ### Check usename exists ###
        # user = Users.query.filter_by(username=username).first()
        # if user:
        #     return 202
        # ### Check email exists ###
        # user = Users.query.filter_by(email=email).first()
        # if user:
        #     # else we can create the user
        #     user = Users(**request.form)
        #     user.create_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
        #     # db.session.add(user)
        #     # db.session.commit()
        #     return 303
        # else:
        #     return 404